/*
 * Copyright (c) 2014 pkzd/1.0, 颜文泽(robin)
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     1 Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     2 Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef MC_ASM_H
#define MC_ASM_H

#include	<types.h>

static inline void
outb(uchar val, ushort port)
{
	asm volatile("outb %1, %0" : : "d"(port), "a"(val));
}

static inline uchar
inb(ushort port)
{
	uchar	val;
	asm volatile("inb %1, %0" : "=a"(val) : "d"(port));
	return val;
}

static inline void
outl(uint val, ushort port)
{
	asm volatile("outl %1, %0" : : "d"(port), "a"(val));
}

static inline uint
inl(ushort port)
{
	uint	val;
	asm volatile("inl %1, %0" : "=a"(val) : "d"(port));
	return val;
}

static inline void
insw(void *addr, ushort port, uint cnt)
{
	asm volatile("cld; rep; insw" : "=D"(addr), "=c"(cnt) : "d"(port), "0"(addr), "1"(cnt));
}

static inline void
outsw(void *addr, ushort port, uint cnt)
{
	asm volatile("cld; rep; outsw" : "=S"(addr), "=c"(cnt) : "d"(port), "0"(addr), "1"(cnt));
}

static inline void
insl(void *addr, ushort port, uint cnt)
{
	asm volatile("cld; rep insl" : "=D"(addr), "=c"(cnt) : "d"(port), "0"(addr), "1"(cnt) : "memory", "cc");
}

static inline void
outsl(void *addr, ushort port, uint cnt)
{
	asm volatile("cld; rep outsl" : "=S"(addr), "=c"(cnt) : "d"(port), "0"(addr), "1"(cnt) : "cc");
}

static inline void
stosb(void *s, int c, int n) 
{
	asm volatile("cld; rep stosb" : "=D"(s), "=c"(n) : "0"(s), "1"(n), "a"(c) : "memory", "cc"); 
}

static inline void
stosl(void *s, int c, int n) 
{
	asm volatile("cld; rep stosl" : "=D"(s), "=c"(n) : "0"(s), "1"(n), "a"(c) : "memory", "cc"); 
}

static inline void
cli(void)
{
	asm volatile("cli" : : );
}

static inline void
sti(void)
{
	asm volatile("sti" : : );
}

static inline void
iret(void)
{
	asm volatile("iret" : : );
}

static inline uint
rcr2(void)
{
	uint val;
  	asm volatile("movl %%cr2, %0" : "=r" (val) : );
  	return val;
}

static inline void
lcr3(uint pgdir)
{
	asm volatile("movl %0, %%cr3" : : "r"(pgdir));
}

static inline void
lidt(uint addr, uint size)
{
	volatile ushort	pd[3];

	pd[0] = size - 1;
	pd[1] = (ushort)addr;
	pd[2] = (ushort)(addr >> 16);
  	asm volatile("lidt (%0)" : : "r" (pd));
}

static inline void
lgdt(uint addr, uint size)
{
	volatile ushort	pd[3];

	pd[0] = size - 1;
	pd[1] = (ushort)addr;
	pd[2] = (ushort)(addr >> 16);
  	asm volatile("lgdt (%0)" : : "r" (pd));
}

static inline void
ltr(ushort sel)
{
	asm volatile("ltr %0" : : "r" (sel));
}

#endif
